import Core.Gadget as Gadget
import Core.Portfolio as Portfolio
from Core.Config import *

import random
from datetime import datetime, timedelta
from pytz import timezone
import pytz
import pandas as pd
import Core.Quote as Quote




# ---Delete Portfolio from Database---
#Portfolio.Delete(database, "TestPortfolio")


# --- Create Portfolio (in Database)---
portfolio = Portfolio.Portfolio("TestPortfolio")
Portfolio.SavePortfolio(database, portfolio)

# --- Add Some Trades---
testSymbol = "000001.SZ"
tradeDateTime = Gadget.GenerateCloseTime(2017,7,13)
Portfolio.Deposit(database, "TestPortfolio", 10000, tradeDateTime)
# Buy
# Add Trades to Database
tradeDateTime = Gadget.GenerateCloseTime(2017,7,14)
quote = Quote.GetQuote(database, testSymbol, tradeDateTime)
shares = int(portfolio._cash / quote["Close"])
Portfolio.AddTrade(database,
                   name="TestPortfolio",
                   symbol=testSymbol,
                   side="Buy",
                   price=quote["Close"],
                   qty=1000,
                   tradeDateTime=tradeDateTime,
                   adjFactor=quote["AdjFactor"])

# ---Caculate Equity with Bar---
beginDateTime = Gadget.GenerateCloseTime(2017,7,13)
endDateTime = Gadget.GenerateCloseTime(2017,7,31)
Portfolio.EquityWithBars(database, "TestPortfolio", beginDateTime, endDateTime)

# portfolio.Summary()

# ---Test Performance---
Portfolio.Performance(database, "TestPortfolio")